package com.ship.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ship.dispatch.bean.SpYearPlanCapacityRecord;
import com.ship.dispatch.bean.SpYearPlanSchemeDetail;
import com.ship.dispatch.bean.SpYearlyPlan;
import com.ship.dispatch.service.SpYearPlanCapacityRecordService;
import com.ship.dispatch.mapper.SpYearPlanCapacityRecordMapper;
import liquibase.pro.packaged.P;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author zhangyh
* @description 针对表【sp_year_plan_capacity_record(资源方年度装载记录表)】的数据库操作Service实现
* @createDate 2024-01-05 15:44:35
*/
@Service
public class SpYearPlanCapacityRecordServiceImpl extends ServiceImpl<SpYearPlanCapacityRecordMapper, SpYearPlanCapacityRecord>
    implements SpYearPlanCapacityRecordService{

    @Override
    public void saveSellerCapacityRecord(String seller, Long planId, SpYearlyPlan lastYearPlan, BigDecimal loadCapacity) {
        SpYearPlanCapacityRecord capacityRecord = this.baseMapper.selectOne(
                new LambdaQueryWrapper<SpYearPlanCapacityRecord>()
                .eq(SpYearPlanCapacityRecord::getPlanId,planId)
                .eq(SpYearPlanCapacityRecord::getSeller,seller)
                .last("limit 1")
        );
        if(capacityRecord != null){
            return;
        }
        SpYearPlanCapacityRecord lastCapacityRecord = null;
        if(lastYearPlan != null){
            //该资源方上一年度的货量剩余
            lastCapacityRecord = this.baseMapper.selectOne(
                    new LambdaQueryWrapper<SpYearPlanCapacityRecord>()
                            .eq(SpYearPlanCapacityRecord::getPlanId,lastYearPlan.getId())
                            .eq(SpYearPlanCapacityRecord::getSeller,seller)
                            .last("limit 1")
            );
        }
        BigDecimal initNum = BigDecimal.ZERO;
        capacityRecord = new SpYearPlanCapacityRecord();
        capacityRecord.setPlanId(planId);
        capacityRecord.setSeller(seller);
        BigDecimal planGoodsCapacity = loadCapacity;
        if(lastCapacityRecord != null && lastCapacityRecord.getBalanceGoodsCapacity() != null){
            planGoodsCapacity = loadCapacity.add(lastCapacityRecord.getBalanceGoodsCapacity());
        }
        capacityRecord.setPlanGoodsCapacity(planGoodsCapacity);
        capacityRecord.setRealGoodsCapacity(planGoodsCapacity);
        capacityRecord.setBalanceGoodsCapacity(initNum);
        this.save(capacityRecord);
    }

    @Override
    public void updateCapacityRecord(List<SpYearPlanSchemeDetail> details) {
        Map<String,List<SpYearPlanSchemeDetail>> sourceSideMap = details.stream().collect(Collectors.groupingBy(SpYearPlanSchemeDetail::getSeller));
        for (String sourceSideName : sourceSideMap.keySet()) {
            List<SpYearPlanSchemeDetail> detailList = sourceSideMap.get(sourceSideName);
            BigDecimal loadCapacity = BigDecimal.ZERO;
            for (SpYearPlanSchemeDetail schemeDetail : detailList) {
                if(schemeDetail.getSingleGoodsQuantity() != null){
                    loadCapacity = loadCapacity.add(schemeDetail.getSingleGoodsQuantity());
                }
            }
            Long planId = detailList.get(0).getPlanId();
            SpYearPlanCapacityRecord capacityRecord = this.baseMapper.selectOne(
                    new LambdaQueryWrapper<SpYearPlanCapacityRecord>()
                            .eq(SpYearPlanCapacityRecord::getPlanId,planId)
                            .eq(SpYearPlanCapacityRecord::getSeller,sourceSideName)
                            .last("limit 1")
            );
            if(capacityRecord != null){
                capacityRecord.setRealGoodsCapacity(loadCapacity);
                capacityRecord.setBalanceGoodsCapacity(capacityRecord.getPlanGoodsCapacity().subtract(loadCapacity));
                this.baseMapper.updateById(capacityRecord);
            }
        }
    }
}




